var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var core = require('core');
var wechat = core.wechat;
var route = require('./route');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.engine('.html', require('ejs').renderFile);
app.set('view engine', 'html');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

core.init(app, {
    db : require('./db'),
    sgin : [{
        path : "/wechat/*",
        method : "POST",
        auth : {
            headers : "sgin"
        }
    },{
        path : "/open/*",
        method : "POST",
        auth : {
            headers : "sgin"
        }
    },{
        path : "/order/*",
        method : "POST",
        auth : {
            headers : "sgin"
        }
    },{
        path : "/db/setuid",
        method : "GET",
        auth : false
    }]
    // {
    //     path : "/cms/*",
    //     method : "GET",
    //     auth : {
    //         headers : "sgin",
    //         db : {
    //             Token : ["token", "uid"]
    //         }
    //     }
    // },{
    //     path : "/cms/login",
    //     method : "POST",
    //     auth : false
    // }
}, function(){
    route.call(app);
});

module.exports = app;
